package com.open.capacity.user.controller;

import com.open.capacity.annotation.log.LogAnnotation;
import com.open.capacity.commons.Result;
import com.open.capacity.user.model.ExcelImport;
import com.open.capacity.user.service.ExcelImportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 作者 ftquan E-mail: 464725602@qq.com
 * @version 创建时间：2017年11月12日 上午22:57:51
 * excel导入管理
 */
@RestController
@Api(tags = "excel导入模块api")
public class ExcelImportController {

    private static Logger logger = LoggerFactory.getLogger(ExcelImportController.class);

    @Autowired
    private ExcelImportService importService;


    /**
     * 查询excel导入
     *
     * @param params
     * @return
     */
    @PreAuthorize("hasAuthority('excelImport:get/excelImports')")
    @ApiOperation(value = "查询excel导入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "功能菜单Id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "keyword", value = "关键字", dataType = "String")
    })
    @GetMapping("/excelImports")
    public Result<List<ExcelImport>> findImports(@RequestParam Map<String, Object> params) {
        List<ExcelImport> list = importService.findImports(params);
        return Result.succeed(list);
    }


    @ApiOperation(value = "获取excel导入")
    @PreAuthorize("hasAuthority('excelImport:get/excelImports/{id}')")
    @GetMapping("/excelImports/{id}")
    public Result<ExcelImport> findImportById(@PathVariable(value = "id") String id) {
        ExcelImport excelImport = importService.findById(id);
        return Result.succeed(excelImport);
    }

    /**
     * 更新状态
     *
     * @param id
     * @author gitgeek
     */
    @PreAuthorize("hasAuthority('excelImport:post/excelImports/{id}/updateStatus' )")
    @PostMapping(value = "/excelImports/{id}/updateStatus")

    public Result updateStatus(@PathVariable(value = "id") String id, Integer status) {
        importService.updateStatus(id, status);
        return Result.succeed(null, "更新成功");
    }


    /**
     * excel导入新增或者更新
     *
     * @param excelImport
     * @return
     */
    @PreAuthorize("hasAnyAuthority('excelImport:post/excelImports','excelImport:put/excelImports')")
    @ApiOperation(value = "保存excel导入")
    @PostMapping("/excelImports/saveOrUpdate")

    public Result saveOrUpdate(@RequestBody ExcelImport excelImport, String keyValue) {
        return importService.saveOrUpdate(excelImport, keyValue);
    }

    /**
     * 删除excel导入
     * delete /excelImports/1
     *
     * @param id
     */
    @PreAuthorize("hasAuthority('excelImport:delete/excelImports/{id}')")
    @ApiOperation(value = "删除excel导入")
    @DeleteMapping("/excelImports/{id}")

    public Result delete(@PathVariable(value = "id") String id) {
        try {
            importService.delete(id);
            return Result.succeed("操作成功");
        } catch (Exception e) {

            return Result.failed("操作失败");
        }
    }

}
